<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\User;
use App\Entity\UserAddress;
use App\Repository\UserAddressRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiUserAddressController extends BaseController4Restful
{
    /**
     * @Route("/api/userAddresss", name="apiGetUserAddresss",methods={"GET","POST"})
     */
    public function apiGetUserAddresss(UserAddressRepository $repository, Request $request)
    {
        $user = $this->needToBeUser();
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
//                dd($data);
//                $data["isDefault"] = $data["isDefault"]."";
//                $data["tags"] = json_decode($data["tags"],true);
                /**@var $UserAddress UserAddress */
                $UserAddress = $this->createEntity($data, UserAddress::class);
                $UserAddress->setUser($user)
                    ->setIsDel(0)->setIsDefault(0);
                $this->myEm()->persist($UserAddress);
                $this->myEm()->flush();
                return $this->json_out(200, "创建成功", $UserAddress);
            default:
                if (!$page = $this->getParamRequired("page")) {
                    $page = 1;
                }
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count([]);
                $this->setMeta($page, $total, $limit);
                $list = $repository->findBy(array("user"=>$user,"isDel"=>0), array("isDefault"=>"DESC","id"=>"DESC"), $limit, $offset);
                return $this->json_out(200, "获取成功", $list);
        }
    }

    /**
     * @Route("/api/userAddresss/{id}", name="apiAddUserAddresss",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateUserAddresss(UserAddress $UserAddress, Request $request)
    {
        $user = $this->needToBeUser();

        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                if ($UserAddress->getUser() != $user) {
                    return $this->json_out(400,"权限不足");
                }
                $UserAddress->setIsDel(1);
                $this->myEm()->persist($UserAddress);
//                $this->myEm()->remove($UserAddress);
                $this->myEm()->flush();
                return $this->json_out(200, "已移除");
            case "PATCH":
            case "PUT":
                if ($UserAddress->getUser() != $user) {
                    return $this->json_out(400,"权限不足");
                }
                $data["tags"] = json_decode($data["tags"],true);
                /**@var $UserAddress UserAddress */
                $UserAddress = $this->updateEntity($data, UserAddress::class, $UserAddress);
                $this->myEm()->persist($UserAddress);
                $this->myEm()->flush();
                break;
            case "Get":
            default:
        }
        return $this->json_out(200, "获取成功", $UserAddress);
    }
}
